<?php
namespace App\Models\System;

use App\Exceptions\ApiException;
use App\Providers\LengthAwarePaginatorProvider;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

/**
* 角色和菜单关联表 模型
* @create 2023-08-21 14:45:47
*/
class SysRoleMenuModel extends \App\Models\BaseModel {
    use HasFactory;

    const Table         = "sys_role_menu";
    const PrimaryKey    = "role_id";
    public $timestamps  = false;

    static $Wheres = ['role_id','menu_id'];     // 直接定义column, 默认where(column, '=', value)
    public function __construct($attributes = []) {
        parent::__construct($attributes);

        // static::$Wheres['role_id']   = fn($q, $v) => $q->where(atn('role_id'), '=', $v);
    }

    /** xxxx */
    // public function user() {
        // return $this->hasOne(User::class, 'id', 'uid');
    // }

    /** ========== 常用查询 ========== */
    static $Vo  = ['*', ];
    static $Voi = ['*', ];

    /**
     * 查询菜单使用数量
     * 
     * @param $menuId 菜单ID
     * @return int 结果
     */
    public function checkMenuExistRole(int $menuId): int {
        return $this
            ->select(static::PrimaryKey)
            ->where('menu_id', '=', $menuId)
            ->count()
            ;
    }

    /**
     * 通过角色ID删除角色和菜单关联
     * 
     * @param $roleId 角色ID
     * @return int 结果
     */
    public function deleteRoleMenuByRoleId(int $roleId): int {
        return $this->deleteRoleMenu([$roleId]);
    }

    /**
     * 批量删除角色菜单关联信息
     * 
     * @param $ids 需要删除的数据ID
     * @return int 结果
     */
    public function deleteRoleMenu(array $ids): int {
        return $this
            ->whereIn('role_id', $ids)
            ->delete();
    }

    /**
     * 批量新增角色菜单信息
     * 
     * @param $roleMenuList 角色菜单列表
     * @return int 结果
     */
    public function batchRoleMenu(array $roleMenuList): int {
        foreach($roleMenuList as $it) {
            $values[] = ['role_id'=>$it['roleId'], 'menu_id'=>$it['menuId']];
        }
        return DB::table(static::Table)
            ->insert($values)
            ;
    }

    /** 根据RoleId获取 */
    public function selectByRoleId($role_id, $menu_id=null): SysRoleMenuModel {
        $where = [['role_id', '=', "{$role_id}"]];
        if ($menu_id) $where[] = ['menu_id', '=', "{$menu_id}"];
        return throw_str(
            static::row($where) ?? "数据($role_id)为空~"
        );
    }
    /** 根据MenuId获取 */
    public function selectByMenuId($menu_id): SysRoleMenuModel {
        $where = [['menu_id', '=', "{$menu_id}"]];
        return throw_str(
            static::row($where) ?? "数据($menu_id)为空~"
        );
    }


    /** 获取列表 @return SysRoleMenu[]+paginator() */
    public function selectList(int|array $pag=1, array $param=[]): LengthAwarePaginatorProvider {
        return static::rows_pag($pag, [], fn($q)=>$q
                    // ->when(isset($param['role_id']), fn($q)=>$q->where('role_id', '=', $param['role_id']))
                    ->wheres($param, 'role_id','menu_id')
                    ->orderByDesc("role_id")
        );
    }


    /** 插入数据 */
    public function insertData(array $data): SysRoleMenuModel {
        return throw_str(
            static::row_add([
                // 'role_id'            => $data['role_id'],
                // 'menu_id'            => $data['menu_id'],
            ])
        );
    }


    /** 更新数据 */
    public function updateData(int|array $idOrWhere, array $data): SysRoleMenuModel {
        $values = static::when(
                    $data, 
                    // 'role_id',
                    // 'menu_id',
                );
        return throw_str(
            static::row_update($idOrWhere, $values)
        );
    }


    /** 删除数据 */
    public function deleteData(int|array $idOrWhere): SysRoleMenuModel {
        return throw_str(
            static::row_delete($idOrWhere)
        );
    }



}